package com.gexiaobao.pigeon.ui.fragment.mall

import android.graphics.Color
import android.graphics.Rect
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.databinding.FragmentMallProductBinding
import com.gexiaobao.pigeon.ui.adapter.MallProductAdapter
import com.gexiaobao.pigeon.ui.mall.MallViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction


/**
 * @Author      : hxw
 * @Date        : 2023/6/12 17:20
 * @Describe    : 产品列表
 */
class FragmentMallProduct : BaseFragment<MallViewModel, FragmentMallProductBinding>() {

    var id = ""
    private val mAdapter: MallProductAdapter by lazy { MallProductAdapter(arrayListOf()) }
    private lateinit var loadsir: LoadService<Any>

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity?.let { StatusBarUtil.setLightMode(it) }
        activity?.let { StatusBarUtil.setColor(it, Color.TRANSPARENT, 0) }
    }

    override fun initView(savedInstanceState: Bundle?) {
        id = requireArguments().getString("id").toString()

        val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        mDatabind.layoutList.swipeRecyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
                super.getItemOffsets(outRect, view, parent, state)
                outRect.right = Util.dp2px(context, 5f)//右间距5dp
                outRect.left = Util.dp2px(context, 5f)//左间距5dp
                outRect.top = Util.dp2px(context, 5f)//上间距5dp
                outRect.bottom = Util.dp2px(context, 5f)//下间距5dp
            }
        })
        mDatabind.layoutList.swipeRecyclerView.layoutManager = layoutManager
        mDatabind.layoutList.swipeRecyclerView.adapter = mAdapter

        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getAppGoodList(id, "1","")
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            loadsir.showLoading()
            mViewModel.getAppGoodList(id, "1","")
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            nav().navigateAction(R.id.action_to_fragment_goods_detail, Bundle().apply {
                putString("id", mAdapter.data[position].id.toString())
            })
        }

        mAdapter.addChildClickViewIds(R.id.llGoToMerchantStore, R.id.riv_item_icon)
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.llGoToMerchantStore -> {
                    nav().navigateAction(R.id.action_to_fragment_merchant_store, Bundle().apply {
                        putInt("shopId", mAdapter.data[position].shopId)
                    })
                }

                R.id.riv_item_icon -> {
                    nav().navigateAction(R.id.action_to_fragment_goods_detail, Bundle().apply {
                        putString("id", mAdapter.data[position].id.toString())
                    })
                }
            }
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        mViewModel.getAppGoodList(id, "1","")
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.goodsListResult.observe(viewLifecycleOwner) {
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
        }
    }

    companion object {
        fun newInstance(id: String): FragmentMallProduct {
            val fragment = FragmentMallProduct()
            val bundle = Bundle()
            fragment.arguments = bundle
            bundle.putString("id", id)
            return fragment
        }
    }
}